package product;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFileChooser;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import stream.MapData;
import stream.MapInput;
import stream.MapList;
import stream.MapUpdate;
import stream.Score;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ProductMain extends JFrame {

	HashMap<String, ProductData> pd = new HashMap<String, ProductData>();
	CodeInput cInput = null;
	CodeUpdate cUpdate = null;
	ProductInput pInput = null;
	ProductUpdate pUpdate = null;
	
	static String filename = "Untitled";
	boolean modifyFlag = false;

	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu mnManage;
	private JMenuItem mntmCodeinput;
	private JMenuItem mntmCodeupdate;
	private JMenuItem mntmProductinput;
	private JMenuItem mntmProductupdate;
	private JMenu mnFile;
	private JMenuItem mntmpLoad;
	private JMenuItem mntmpSave;
	private JMenuItem mntmpSaveAs;
	private JMenuItem mntmAll;
	
	int yn;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProductMain frame = new ProductMain();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ProductMain() {
		setTitle("Product Manage Program");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 961, 693);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();		
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

	}

	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnFile());
			menuBar.add(getMnManage());
		}
		return menuBar;
	}
	private JMenu getMnManage() {
		if (mnManage == null) {
			mnManage = new JMenu("Manage");
			mnManage.add(getMntmAll());
			mnManage.add(getMntmCodeinput());
			mnManage.add(getMntmCodeupdate());
			mnManage.add(getMntmProductinput());
			mnManage.add(getMntmProductupdate());
		}
		return mnManage;
	}
	private JMenuItem getMntmCodeinput() {
		if (mntmCodeinput == null) {
			mntmCodeinput = new JMenuItem("CodeInput");
			mntmCodeinput.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(cInput == null){
						cInput = new CodeInput();
						cInput.setD(pd,modifyFlag);
						cInput.setBounds(0, 0, 239, 158);
					}
					contentPane.add(cInput);
					cInput.setVisible(true);
				}
			});
		}
		return mntmCodeinput;
	}
	private JMenuItem getMntmCodeupdate() {
		if (mntmCodeupdate == null) {
			mntmCodeupdate = new JMenuItem("CodeUpdate");
			mntmCodeupdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(cUpdate == null){						
						cUpdate = new CodeUpdate();
						cUpdate.setD(pd,modifyFlag);
						cUpdate.setBounds(0,158,354, 384);
					}
					contentPane.add(cUpdate);
					cUpdate.setVisible(true);

				}
			});
		}
		return mntmCodeupdate;
	}
	private JMenuItem getMntmProductinput() {
		if (mntmProductinput == null) {
			mntmProductinput = new JMenuItem("ProductInput");
			mntmProductinput.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(pInput == null){
						pInput = new ProductInput();
						pInput.setD(pd,modifyFlag);
						pInput.setBounds(384, 0, 335, 173);
					}
					contentPane.add(pInput);
					pInput.setVisible(true);
				}
			});
		}
		return mntmProductinput;
	}
	private JMenuItem getMntmProductupdate() {
		if (mntmProductupdate == null) {
			mntmProductupdate = new JMenuItem("ProductUpdate");
			mntmProductupdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(pUpdate == null){
						pUpdate = new ProductUpdate();
						pUpdate.setD(pd,modifyFlag);
						pUpdate.setBounds(384, 173, 463, 415);						
					}
					contentPane.add(pUpdate);
					pUpdate.setVisible(true);

				}
			});
		}
		return mntmProductupdate;
	}
	private JMenu getMnFile() {
		if (mnFile == null) {
			mnFile = new JMenu("Product File");
			mnFile.add(getMntmpLoad());
			mnFile.add(getMntmpSave());
			mnFile.add(getMntmpSaveAs());
		}
		return mnFile;
	}
	private JMenuItem getMntmpLoad() {
		if (mntmpLoad == null) {
			mntmpLoad = new JMenuItem("Load");
			mntmpLoad.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						loadP();
					}catch(Exception ex){
						JOptionPane.showMessageDialog(null, "알수없는 에러가 발생하였습니다.");
					}
				}
			});
		}
		return mntmpLoad;
	}
	private JMenuItem getMntmpSave() {
		if (mntmpSave == null) {
			mntmpSave = new JMenuItem("Save");
			mntmpSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						saveP();
					}catch(Exception ex){
						JOptionPane.showMessageDialog(null, "알수없는 에러가 발생하였습니다.");
					}
				}
			});
		}
		return mntmpSave;
	}
	private JMenuItem getMntmpSaveAs() {
		if (mntmpSaveAs == null) {
			mntmpSaveAs = new JMenuItem("SaveAs");
			mntmpSaveAs.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						saveAsP();
					}catch(Exception ex){
						JOptionPane.showMessageDialog(null, "알수없는 에러가 발생하였습니다.");
					}
				}
			});
		}
		return mntmpSaveAs;
	}
	// 메뉴에서 모든 창 한번에 다 열어지도록 구현.
	private JMenuItem getMntmAll() {
		if (mntmAll == null) {
			mntmAll = new JMenuItem("All");
			mntmAll.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(cInput == null){
						cInput = new CodeInput();
						cInput.setD(pd,modifyFlag);
						cInput.setBounds(0, 0, 239, 158);
					}
					contentPane.add(cInput);
					cInput.setVisible(true);
					
					if(cUpdate == null){						
						cUpdate = new CodeUpdate();
						cUpdate.setD(pd,modifyFlag);
						cUpdate.setBounds(0,158,354, 384);
					}
					contentPane.add(cUpdate);
					cUpdate.setVisible(true);
					
					if(pInput == null){
						pInput = new ProductInput();
						pInput.setD(pd,modifyFlag);
						pInput.setBounds(384, 0, 335, 173);
					}
					contentPane.add(pInput);
					pInput.setVisible(true);
					
					if(pUpdate == null){
						pUpdate = new ProductUpdate();
						pUpdate.setD(pd,modifyFlag);
						pUpdate.setBounds(384, 173, 463, 415);						
					}
					contentPane.add(pUpdate);
					pUpdate.setVisible(true);
				}
			});
		}
		return mntmAll;
	}
	public void loadP() throws Exception{
		int yn=-1;
		
		if(modifyFlag){ // 현재 파일 내용이 수정된 경우
			yn = JOptionPane.showConfirmDialog(null, "파일이 수정되었습니다. 저장 하시겠습니까?");
			if(yn == JOptionPane.YES_OPTION) saveP();
		}
		
		// 저장된 이후이거나, 파일이 없거나...
		JFileChooser fc = new JFileChooser();
		yn = fc.showOpenDialog(this);		
		if(yn == JFileChooser.APPROVE_OPTION){ // 열기 버튼 클릭시
			File f = fc.getSelectedFile();
			
			filename = f.getPath();
			setTitle("Filename : " + filename);
			
			FileInputStream fis = new FileInputStream(filename);
			ObjectInputStream ois = new ObjectInputStream(fis);
			pd = (HashMap<String,ProductData>)ois.readObject();
			
			if(cInput != null) cInput.setD(pd,modifyFlag);
			if(cUpdate != null) cUpdate.setD(pd,modifyFlag);
			if(pInput != null) pInput.setD(pd,modifyFlag);
			if(pUpdate != null) pUpdate.setD(pd,modifyFlag);		
					
			ois.close();
			fis.close(); // 닫을때는 역순으로.
			
			JOptionPane.showMessageDialog(null, "Your File Safely Loaded.");			
			modifyFlag = false;
		}
	}
	public void saveP() throws Exception{
		if(filename.equals("Untitled")){ // 파일명이 정해지지 않았을때
			saveAsP();
		}
		if(yn == JFileChooser.CANCEL_OPTION){
			JOptionPane.showMessageDialog(null, "Save is Canceled");
		}else{
			FileOutputStream fos = new FileOutputStream(filename);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			
			oos.writeObject(pd);
			JOptionPane.showMessageDialog(null, "Your File is Safely Saved.");		
			oos.close();
			fos.close();
			setTitle("Filename : " + filename);
			modifyFlag = false;
		}		
	}
	public void saveAsP() throws Exception{
		JFileChooser fc = new JFileChooser(filename);
		yn = fc.showSaveDialog(this);
		if(yn == JFileChooser.APPROVE_OPTION){
			File f = fc.getSelectedFile();
			filename = f.getPath();
			saveP();
		}
	}
}
